<script>
$(document).ready(function () {
    //添加
    $("#add-dict-sub").click(function () {
        var key = $("#key").val();
        var val = $("#val").val();
        var msg = $("#msg").val();
        $.ajax({
            type:"POST",
            async:false,
            url:"/rbac/addict/",
            data:{"key":key,"val":val,"msg":msg},
            dataType : "json",
            success:function (data) {
                $("#addDictModal").modal('hide');
                spop({
                    template: data,
                    style: 'success',
                    autoclose: 2000
                });
                setTimeout("location.reload()",2100);
            },
            error:function (data) {
                spop({
                    template:data.responseText,
                    style: 'error',
                    autoclose: 2000
                });
            }
        })
    });

    //获取修改内容
    $("td>a[name='edit-dict']").click(function () {
        var id = $(this).attr("dictid");
        $.ajax({
            type: "POST",
            async: false,
            url: "/rbac/iddict/",
            data:{"dict_id":id },
            dataType: "json",
            success:function (data) {
                $("#edit-key").val(data.DictKey);
                $("#edit-val").val(data.DictVal);
                $("#edit-msg").val(data.DictMsg);
                $("#edit-dict-sub").attr("dict_id",data.ID);
                $("#editDictModal").modal("show");
            },
            error:function (data) {
                spop({
                    template:data.responseText,
                    style: 'error',
                    autoclose: 2000
                });
            }
        });


    });

    //提交角色修改内容
    $("#edit-dict-sub").click(function () {
        var dict_id = $(this).attr("dict_id");
        var key = $("#edit-key").val();
        var val = $("#edit-val").val();
        var msg = $("#edit-msg").val();
        $.ajax({
            type:"PUT",
            async:false,
            url:"/rbac/editdict/",
            data:{"dict_id":dict_id,"key":key,"val":val,"msg":msg},
            dataType : "json",
            success:function (data) {
                $("#editDictModal").modal('hide');
                spop({
                    template: data,
                    style: 'success',
                    autoclose: 2000
                });
                setTimeout("location.reload()",2100);
            },
            error:function (data) {
                spop({
                    template:data.responseText,
                    style: 'error',
                    autoclose: 2000
                });
            }
        })
    });
    //删除角色
    $("td>a[name='del-dict']").click(function () {
        var dict_id = $(this).attr('dictid');
        var statu = confirm("是否确认删除!");
        if (statu==true)
        {
            $.ajax({
                type: "post",
                async:false,
                url: "/rbac/deldict/",
                data: {"dict_id":dict_id},
                dataType : "json",
                success: function(data) {
                    spop({
                        template:data,
                        style: 'warning',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()",2100);
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            });
        }
    });

    //搜索角色
    $("#search-dict").bind('change',function () {
        var key = $(this).val();
        if (key != "" ) {
            var url = "/rbac/searchdict/" + key + "/";

        }else{
            var url = "/rbac/dict/";
        }
        $("#search-dict-a").attr("href", url);
    });

})
</script>
